package com.wd.mylibrary_square.view.activity

import android.content.Intent
import android.opengl.Visibility
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.alibaba.android.arouter.facade.annotation.Route
import com.ayvytr.adapter.smart
import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_base.bean.SearchBean
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_base.utils.RoomBean
import com.wd.mylibrary_base.utils.RoomDB
import com.wd.mylibrary_square.R
import com.wd.mylibrary_square.databinding.ActivitySearchBinding
import com.wd.mylibrary_square.viewmodel.SearchViewModel
import androidx.room.Room

@Route(path = "/Search/Search")
class SearchActivity : BaseActivity<SearchViewModel,ActivitySearchBinding>(R.layout.activity_search) {

    var handler:Handler= Handler()

    //搜索直播列表适配器
    var searchListAdapter=smart(mutableListOf<SearchBean.Result>(),R.layout.item_search,{ item, position ->
        findViewById<ImageView>(R.id.headPic).load(item.headPic){
            error(R.mipmap.ic_launcher)
            placeholder(R.mipmap.ic_launcher)
            transformations(RoundedCornersTransformation(360f))
        }

        findViewById<TextView>(R.id.nickName).text=item.nickName

        if(item.liveStatus==1){
            findViewById<TextView>(R.id.state_live).text=" 直播中"
        }else{
            findViewById<TextView>(R.id.state_live).text=" 打样中"
        }

    }){}.click { item, position ->

    }
    override fun initView() {
        var userId = MyApp.sp.getInt("userId",0)
        //获取输入框内容
        var text = dataBinding.text.text.toString()

        viewModel.getSearchData(userId,text,1,5)

        dataBinding.rvSearch.adapter=searchListAdapter
        dataBinding.rvSearch.layoutManager=LinearLayoutManager(this)
        dataBinding.rvSearch.addItemDecoration(DividerItemDecoration(this,LinearLayoutManager(this).orientation))

        getRoom(text,false)

        dataBinding.delete.setOnClickListener {
            getRoom(text,true)
        }

        dataBinding.text.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                searchListAdapter.clear()
                var text = dataBinding.text.text.toString()
                viewModel.getSearchData(userId,text,1,5)

                var view = Button(this@SearchActivity)
                if(text!=null){
                    dataBinding.view.addView(view,0)
                }

                getRoom(text,false)
            }

        })
    }

    fun getRoom(text:String,flag:Boolean){
        //获取room实例
        var db =
            Room.databaseBuilder(this, RoomDB::class.java,"RoomSearch").build()
        var dao=db.getDao()

        //数据库 操作
        Thread{
            dao!!.deleteById(text)
            dao?.add(RoomBean(text))
            if(flag){
                dao.deleteAll()
            }

            var list=dao?.queryAll()
            handler.post{
                dataBinding.view.removeAllViews()
                for (i in list!!){
                    var view = Button(this@SearchActivity)

                    view.text=i.getContent()

                    if(text!=null){
                        dataBinding.view.addView(view,0)
                    }

                    //点击 给输入框赋值
                    view.setOnClickListener {
                        dataBinding.text.setText(view.text)
                    }

//                            长按删除
//                            view.setOnLongClickListener({
//
//                            })
                }
            }
        }.start()
    }

    override fun dataObserve() {
        viewModel.searchData.observe(this,{

            //判断显示与否
            if(dataBinding.text.text.isEmpty()){

                dataBinding.historyNo.visibility=View.GONE
                dataBinding.rvSearch.visibility=View.GONE
                dataBinding.history.visibility=View.VISIBLE
            }else{
                if(!it.result.isEmpty()){
                    searchListAdapter.update(it.result,true)
                     dataBinding.history.visibility=View.GONE
                    dataBinding.historyNo.visibility=View.GONE
                    dataBinding.rvSearch.visibility=View.VISIBLE

                } else{
                    dataBinding.history.visibility=View.GONE
                    dataBinding.rvSearch.visibility=View.GONE
                    dataBinding.historyNo.visibility=View.VISIBLE
                }
            }

        })
    }
}